//
//  GoodsDetailTitleTableViewCell.m
//  much
//
//  Created by weijunjie on 2019/1/7.
//  Copyright © 2019年 iOS. All rights reserved.
//

#import "GoodsDetailTitleTableViewCell.h"
#import "GoodsModel.h"

@interface GoodsDetailTitleTableViewCell ()
@property (nonatomic, strong) UILabel *storeNowPriceLabel; // 商品当前价格
@property (nonatomic, strong) UILabel *storeTitleLabel; // 商品标题Label
@property (nonatomic, strong) UILabel *storeDescribeLabel; // 商品描述Label
@property (nonatomic, strong) UILabel *markLabel; // 商品标题标签
@property (nonatomic, strong) UILabel *lineActionPriceLabel; // 划线价
@property (nonatomic, strong) UILabel *singleBuyLabel; // 市场均价
@property (nonatomic, strong) UILabel *discountLabel; // 折扣
@end
static NSString *GoodsDetailTitleTableViewCellIdentifier = @"GoodsDetailTitleTableViewCellIdentifier";
@implementation GoodsDetailTitleTableViewCell
+ (instancetype)detailTitleTableViewCellWithTableView:(UITableView *)tableView {
    GoodsDetailTitleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:GoodsDetailTitleTableViewCellIdentifier];
    if (!cell) {
        cell = [[GoodsDetailTitleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:GoodsDetailTitleTableViewCellIdentifier];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setModel:(GoodsModel *)model {
    _model = model;
    
    if (model.sellingPrice.hasValue) {
        // 价格
        NSString *nowPriceText = [NSString stringWithFormat:@"¥ %@", model.sellingPrice];
        NSMutableAttributedString *nowPriceAttr = [[NSMutableAttributedString alloc] initWithString:nowPriceText];
        [nowPriceAttr addAttribute:NSFontAttributeName value:APPFONT(15) range:NSMakeRange(0, 1)];
        [nowPriceAttr addAttribute:NSFontAttributeName value:APPBOLDPINGFANGFONT(23) range:NSMakeRange(1, nowPriceText.length - 1)];
        self.storeNowPriceLabel.attributedText = nowPriceAttr;
    }

    // 划线价
    if (model.marketSellingPrice.hasValue) {
        NSMutableAttributedString *newPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥ %@",model.marketSellingPrice]];
        [newPrice addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, newPrice.length)];
        self.lineActionPriceLabel.attributedText = newPrice;
        self.discountLabel.text = [NSString stringWithFormat:@"%@折",model.sellingDiscount];
    }

    // 标题
    if (model.overseas && model.title.hasValue) {
        self.markLabel.hidden = NO;
        NSMutableParagraphStyle *muParagraph = [[NSMutableParagraphStyle alloc]init];
        muParagraph.lineSpacing = 2;
        muParagraph.firstLineHeadIndent = 40;
        
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:model.title attributes:@{NSParagraphStyleAttributeName : muParagraph}];
        self.storeTitleLabel.attributedText =  attr;
        CGFloat titleH = [@"跨境" boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : APPFONT(14)} context:nil].size.height;
        [self.markLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.storeTitleLabel).with.offset(2);
            make.left.equalTo(self.storeTitleLabel);
            make.width.mas_equalTo(33);
            make.height.mas_equalTo(titleH);
        }];
    } else {
        self.markLabel.hidden = YES;
        self.storeTitleLabel.text = model.title;
    }
    
    // 描述
    self.storeDescribeLabel.text = model.subTitle;
    
}

#pragma mark - 懒加载
/** 当前价格 */
- (UILabel *)storeNowPriceLabel {
    if (!_storeNowPriceLabel) {
        _storeNowPriceLabel = [[UILabel alloc] init];
        _storeNowPriceLabel.textColor = Color175030;
        _storeNowPriceLabel.font = APPFONT(23);
        [self.contentView addSubview:_storeNowPriceLabel];
        [_storeNowPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(15);
            make.top.equalTo(self.contentView).offset(19);
        }];
    }
    return _storeNowPriceLabel;
}

- (UILabel *)lineActionPriceLabel {
    if (!_lineActionPriceLabel) {
        UIFont *font = APPFONT(12);
        UIColor *color = Color159;
        UILabel *singleBuyLabel = [[UILabel alloc] init];
        singleBuyLabel.font = font;
        singleBuyLabel.textColor = color;
        singleBuyLabel.text = @"市场均价 ";
        [self addSubview:singleBuyLabel];
        _singleBuyLabel = singleBuyLabel;
        [singleBuyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.storeNowPriceLabel.mas_right).offset(6);
            make.top.equalTo(self.storeNowPriceLabel).offset(7) ;
        }];
        
        _lineActionPriceLabel = [[UILabel alloc] init];
        _lineActionPriceLabel.font = font;
        _lineActionPriceLabel.textColor = color;
        [self addSubview:_lineActionPriceLabel];
        [_lineActionPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(singleBuyLabel.mas_right);
            make.centerY.equalTo(singleBuyLabel);
        }];
    }
    return _lineActionPriceLabel;
}


- (UILabel *)discountLabel {
    if (!_discountLabel) {
        _discountLabel = [[UILabel alloc] init];
        _discountLabel.font = APPFONT(12);
        _discountLabel.textColor = Color159;
        [self addSubview:_discountLabel];
        [_discountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.lineActionPriceLabel);
            make.left.equalTo(self.lineActionPriceLabel.mas_right).offset(7);
        }];
    }
    return _discountLabel;
}

/** 商品标题 */
- (UILabel *)storeTitleLabel {
    if (!_storeTitleLabel) {
        _storeTitleLabel = [[UILabel alloc] init];
        _storeTitleLabel.font = APPFONT(16);
        _storeTitleLabel.textColor = Color34;
        _storeTitleLabel.numberOfLines = 2;
        [self.contentView addSubview:_storeTitleLabel];
        [_storeTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.storeNowPriceLabel.mas_bottom).with.offset(10);
            make.left.equalTo(self.storeNowPriceLabel);
            make.right.equalTo(self.contentView).offset(-15);
        }];
    }
    return _storeTitleLabel;
}

/** 商品描述 */
- (UILabel *)storeDescribeLabel {
    if (!_storeDescribeLabel) {
        _storeDescribeLabel = [UILabel new];
        _storeDescribeLabel.textColor = Color140;
        _storeDescribeLabel.font = APPFONT(14);
        _storeDescribeLabel.numberOfLines = 2;
        [self.contentView addSubview:self.storeDescribeLabel];
        [_storeDescribeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.storeTitleLabel.mas_bottom).with.offset(3);
            make.left.equalTo(self.storeTitleLabel);
            make.right.equalTo(self.contentView).offset(-15);
        }];
    }
    return _storeDescribeLabel;
}

/** 商品标题标签文字 */
- (UILabel *)markLabel {
    if (!_markLabel) {
        _markLabel = [[UILabel alloc] init];
        _markLabel.text = @"跨境";
        _markLabel.font = APPFONT(14);
        _markLabel.textAlignment = NSTextAlignmentCenter;
        _markLabel.backgroundColor = [UIColor clearColor];
        _markLabel.layer.borderWidth = 0.5;
        _markLabel.layer.cornerRadius = 2;
        _markLabel.layer.masksToBounds = YES;
        _markLabel.textColor = Color175030;
        _markLabel.layer.borderColor = Color175030.CGColor;
        [self.contentView addSubview:_markLabel];
    }
    return _markLabel;
}

+ (CGFloat)calculateHeightWithModel:(GoodsModel *)model {
    CGFloat h = 19;
    
    // 计算价格的高度
    h += [@"56" boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : APPFONT(23)} context:nil].size.height + 10;
    
    // 计算标题的高度
    CGFloat titleMaxH = [@"标\n题" boundingRectWithSize:CGSizeMake(ScreenWidth - 30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : APPFONT(16)} context:nil].size.height;
    CGFloat titleH = [model.title boundingRectWithSize:CGSizeMake(ScreenWidth - 30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : APPFONT(16)} context:nil].size.height;
    if (titleH > titleMaxH) {
        titleH = titleMaxH;
    }
    
    h += titleH + 3;
    
    //计算商品描述的高度
    CGFloat desMaxH = [@"描\n述" boundingRectWithSize:CGSizeMake(ScreenWidth - 30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : APPFONT(14)} context:nil].size.height;
    CGFloat desH = [model.subTitle boundingRectWithSize:CGSizeMake(ScreenWidth - 30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : APPFONT(14)} context:nil].size.height;
    if (desH > desMaxH) {
        desH = desMaxH;
    }
    
    h += desH + 17;
    return h;
}

@end
